/* Copyright Homeaway, Inc 2005-2008. All Rights Reserved.
 * No unauthorized use of this software.
 */
package org.mis6323.data.dao.impl;


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractFileBasedDaoImpl<T> {
    protected String fileName;
    protected List<T> data;

    public AbstractFileBasedDaoImpl(String fileName) {
        this.fileName = fileName;
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(this.fileName));
            this.data = (List<T>) ois.readObject();
            ois.close();
        } catch (Exception e) {
            this.data = new ArrayList<T>();
        }
    }

    public void flush() {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(this.fileName));
            oos.writeObject(this.data);
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
